package com.xhd.util;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DataFilter {
	public static String filterStringToDB(String paramString)
	  {
	    if ((paramString == null) || (paramString.equals("")))
	      return "";
	    StringBuffer localStringBuffer = new StringBuffer("");
	    char c = '\'';
	    while (true)
	    {
	      int i = paramString.indexOf(c);
	      if (i >= 0)
	      {
	        localStringBuffer.append(paramString.substring(0, i + 1));
	        localStringBuffer.append(c);
	        paramString = paramString.substring(i + 1, paramString.length());
	      }
	      localStringBuffer.append(paramString);
	      break;
	    }
	    return localStringBuffer.toString();
	  }

	  public static String filterStringToDB(Object paramObject)
	  {
	    if (paramObject == null)
	      return "";
	    String str = "";
	    try
	    {
	      str = (String)paramObject;
	    }
	    catch (Exception localException)
	    {
	      str = paramObject.toString();
	    }
	    return filterStringToDB(str);
	  }

	  public static String filterFloatToDB(double paramDouble)
	  {
	    String str = String.valueOf(paramDouble);
	    if ((str == null) || (str.length() < 1))
	      return "0";
	    if (str.length() > 38)
	      return str.substring(0, 38);
	    return str;
	  }

	  public static String filterFloatToDB(Object paramObject)
	  {
	    if (paramObject == null)
	      return "0";
	    String str = paramObject.toString();
	    if (str.length() < 1)
	      return "0";
	    if (str.length() > 38)
	      return str.substring(0, 38);
	    return str;
	  }

	  public static String filterObjToSQLValue(Object paramObject, int paramInt)
	  {
	    if (paramObject == null)
	      return "null";
	    Object localObject1;
	    Object localObject2;
	    if (paramObject instanceof String)
	    {
	      localObject1 = (String)paramObject;
	      localObject2 = new StringBuffer(((String)localObject1).length() + 8);
	      ((StringBuffer)localObject2).append('\'');
	      ((StringBuffer)localObject2).append(((String)localObject1).replaceAll("'", "''"));
	      ((StringBuffer)localObject2).append('\'');
	      return ((StringBuffer)localObject2).toString();
	    }
	    if (paramObject instanceof BigDecimal)
	    {
	      localObject1 = paramObject.toString();
	      if (((String)localObject1).length() > 38)
	        localObject1 = ((String)localObject1).substring(0, 38);
	      return localObject1.toString();
	    }
	    if (paramObject instanceof Date)
	    {
	      localObject1 = new StringBuffer(32);
	      if (paramInt == 1)
	      {
	        localObject2 = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
	        ((StringBuffer)localObject1).append("to_date('");
	        ((StringBuffer)localObject1).append(((SimpleDateFormat)localObject2).format(paramObject));
	        ((StringBuffer)localObject1).append("','MM/DD/YYYY HH24:MI:SS')");
	      }
	      else
	      {
	        localObject2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        ((StringBuffer)localObject1).append('\'');
	        ((StringBuffer)localObject1).append(((SimpleDateFormat)localObject2).format(paramObject));
	        ((StringBuffer)localObject1).append('\'');
	      }
	      return ((StringBuffer)localObject1).toString();
	    }
	    return ((String)(String)paramObject.toString());
	  }

	  public static String filterStringToXML(String paramString)
	  {
	    if (paramString != null)
	    {
	      StringBuffer localStringBuffer = new StringBuffer(paramString.length());
	      int i = 0;
	    /*  break label100:*/
	      while (true)
	      {
	        char c = paramString.charAt(i);
	        if (c == '"')
	          localStringBuffer.append("&quot;");
	        else if (c == '<')
	          localStringBuffer.append("&lt;");
	        else if (c == '>')
	          localStringBuffer.append("&gt;");
	        else if (c == '&')
	          localStringBuffer.append("&amp;");
	        else
	          localStringBuffer.append(c);
	        ++i;
	        if (i >= paramString.length())
	          label100: return localStringBuffer.toString();
	      }
	    }
	    return "";
	  }

	  public static String filterToHtml(String paramString)
	  {
	    if (paramString == null)
	      return "";
	    StringBuffer localStringBuffer = new StringBuffer(paramString.length());
	    int i = paramString.length();
	    for (int j = 0; j < i; ++j)
	    {
	      char c = paramString.charAt(j);
	      if (c == '"')
	      {
	        localStringBuffer.append("&quot;");
	      }
	      else if (c == '<')
	      {
	        localStringBuffer.append("&lt;");
	      }
	      else if (c == '>')
	      {
	        localStringBuffer.append("&gt;");
	      }
	      else if (c == '&')
	      {
	        localStringBuffer.append("&amp;");
	      }
	      else if (c == '\n')
	      {
	        localStringBuffer.append("<br>");
	      }
	      else if (c == '\r')
	      {
	        if ((j + 1 != i) && ('\n' == paramString.charAt(j + 1)))
	          continue;
	        localStringBuffer.append("<br>");
	      }
	      else
	      {
	        localStringBuffer.append(c);
	      }
	    }
	    return localStringBuffer.toString();
	  }
}
